package com.ruoyi.project.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.domain.ProProject;
import com.ruoyi.project.domain.ProProjectContract;
import com.ruoyi.project.mapper.ProProjectContractMapper;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ConContractMapper;
import com.ruoyi.project.domain.ConContract;
import com.ruoyi.project.service.IConContractService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同Service业务层处理
 * 
 * @author phil
 * @date 2020-05-29
 */
@Service
public class ConContractServiceImpl implements IConContractService{
	@Autowired
	private ConContractMapper conContractMapper;

	@Autowired
	private ProProjectContractMapper proProjectContractMapper;

	/**
	 * 查询合同
	 * 
	 * @param contractId 合同ID
	 * @return 合同
	 */
	@Override
	public ConContract selectConContractById(Long contractId){
		return conContractMapper.selectConContractById(contractId);
	}

	/**
	 * 查询合同列表
	 * 
	 * @param conContract 合同
	 * @return 合同
	 */
	@Override
	public List<ConContract> selectConContractList(ConContract conContract){
		return conContractMapper.selectConContractList(conContract);
	}

	/**
	 * 新增合同
	 * 
	 * @param conContract 合同
	 * @return 结果
	 */
	@Override
	public int insertConContract(ConContract conContract){
		conContract.setCreateTime(DateUtils.getNowDate());
		return conContractMapper.insertConContract(conContract);
	}

	/**
	 * 修改合同
	 * 
	 * @param conContract 合同
	 * @return 结果
	 */
	@Override
	public int updateConContract(ConContract conContract){
		conContract.setUpdateTime(DateUtils.getNowDate());
		return conContractMapper.updateConContract(conContract);
	}

	/**
	 * 删除合同对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteConContractByIds(String ids){
		return conContractMapper.deleteConContractByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除合同信息
	 * 
	 * @param contractId 合同ID
	 * @return 结果
	 */
	@Override
	public int deleteConContractById(Long contractId){
		return conContractMapper.deleteConContractById(contractId);
	}

	/**
	 *
	 * @param project
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getContractByProjectId(ProProject project){
		return conContractMapper.getContractByProjectId(project);
	}

	@Override
	@Transactional
	public int insertConContracts(List<ConContract> contractList){
		int resultCount = 0;
		if(contractList != null && contractList.size() > 0){
			for(ConContract contract : contractList){
				resultCount += conContractMapper.insertConContract(contract);
				String projectIds = contract.getProjectIds();
				if(!"".equals(projectIds)){ // 插入合同与项目关系表
					String[] projectIdArray = projectIds.split(",");
					if(projectIdArray != null && projectIds.length() > 0){
						for(String projectId : projectIdArray){
							ProProjectContract ppc = new ProProjectContract();
							ppc.setProId(NumberUtils.toLong(projectId));
							ppc.setConId(contract.getContractId());
							ppc.setCreateBy(contract.getCreateBy());
							ppc.setCreateTime(new Date());
							proProjectContractMapper.insertProProjectContract(ppc);
						}
					}
				}
			}
		}
		return resultCount;
	}
}
